import { Controller, Get, Post, Body, Query, Request, Put, Res, HttpCode, Param, Delete } from '@nestjs/common';
import { ApiTags, ApiOperation, ApiBody, ApiConsumes, ApiQuery, ApiBearerAuth, ApiParam } from '@nestjs/swagger';
import { DictService } from './dict.service';
import { CreateDictTypeDto, UpdateDictTypeDto, ListDictType, CreateDictDataDto, UpdateDictDataDto, ListDictData } from './dto/index';
import { RequirePermission } from 'src/common/decorators/require-premission.decorator';
import { Response } from 'express';
import { Operlog } from 'src/common/decorators/operlog.decorator';
import { BusinessType } from 'src/common/constant/business.constant';
import { RepeatSubmit } from 'src/common/decorators/repeat-submit.decorator';
import { UserTool, UserToolType } from '../user/user.decorator';

@ApiTags('字典管理')
@Controller('system/dict')
export class DictController {
  constructor(private readonly dictService: DictService) {}

  //字典类型
  @ApiOperation({ summary: '字典管理创建' })
  @ApiBody({ type: CreateDictTypeDto, required: true })
  @RequirePermission('system:dict:add')
  @HttpCode(200)
  @Post('/type')
  @RepeatSubmit()
  @Operlog({ businessType: BusinessType.INSERT })
  createType(@Body() createDictTypeDto: CreateDictTypeDto, @UserTool() { injectCreate }: UserToolType) {
    return this.dictService.createType(injectCreate(createDictTypeDto));
  }

  @ApiOperation({ summary: '字典数据-刷新缓存' })
  @RequirePermission('system:dict:remove')
  @Delete('/type/refreshCache')
  refreshCache() {
    return this.dictService.resetDictCache();
  }

  @ApiOperation({ summary: '字典管理删除' })
  @RequirePermission('system:dict:remove')
  @Delete('/type/:id')
  @ApiParam({ name: 'id', required: true, description: '字典类型id' })
  deleteType(@Param('id') ids: string) {
    const dictIds = ids.split(',').map((id) => +id);
    return this.dictService.deleteType(dictIds);
  }

  @ApiOperation({ summary: '字典管理修改' })
  @ApiBody({ type: UpdateDictTypeDto, required: true })
  @RequirePermission('system:dict:edit')
  @RepeatSubmit()
  @Operlog({ businessType: BusinessType.UPDATE })
  @Put('/type')
  updateType(@Body() updateDictTypeDto: UpdateDictTypeDto, @UserTool() { injectUpdate }: UserToolType) {
    return this.dictService.updateType(injectUpdate(updateDictTypeDto));
  }

  @ApiOperation({ summary: '字典管理列表' })
  @RequirePermission('system:dict:list')
  @ApiQuery({ type: ListDictType, required: false })
  @Get('/type/list')
  findAllType(@Query() query: ListDictType) {
    return this.dictService.findAllType(query);
  }

  @ApiOperation({ summary: '全部字典类型-下拉数据' })
  @RequirePermission('system:dict:query')
  @Get('/type/optionselect')
  findOptionSelect() {
    return this.dictService.findOptionselect();
  }

  @ApiOperation({ summary: '字典管理详情' })
  @RequirePermission('system:dict:query')
  @Get('/type/:id')
  @ApiParam({ name: 'id', required: true, description: '字典类型id' })
  findOneType(@Param('id') id: string) {
    return this.dictService.findOneType(+id);
  }

  // 字典数据
  @ApiOperation({ summary: '字典数据-创建' })
  @RequirePermission('system:dict:add')
  @ApiBody({ type: CreateDictDataDto, required: true })
  @HttpCode(200)
  @Post('/data')
  @RepeatSubmit()
  @Operlog({ businessType: BusinessType.INSERT })
  createDictData(@Body() createDictDataDto: CreateDictDataDto, @UserTool() { injectUpdate }: UserToolType) {
    return this.dictService.createDictData(injectUpdate(createDictDataDto));
  }

  @ApiOperation({ summary: '字典数据-删除' })
  @RequirePermission('system:dict:remove')
  @Delete('/data/:id')
  @ApiParam({ name: 'id', required: true, description: '字典数据id' })
  @Operlog({ businessType: BusinessType.DELETE })
  deleteDictData(@Param('id') ids: string) {
    const dictIds = ids.split(',').map((id) => +id);
    return this.dictService.deleteDictData(dictIds);
  }

  @ApiOperation({ summary: '字典数据-修改' })
  @RequirePermission('system:dict:edit')
  @ApiBody({ type: UpdateDictDataDto, required: true })
  @Put('/data')
  @RepeatSubmit()
  @Operlog({ businessType: BusinessType.UPDATE })
  updateDictData(@Body() updateDictDataDto: UpdateDictDataDto, @UserTool() { injectUpdate }: UserToolType) {
    return this.dictService.updateDictData(injectUpdate(updateDictDataDto));
  }

  @ApiOperation({ summary: '字典数据-列表' })
  @RequirePermission('system:dict:list')
  @ApiQuery({ type: ListDictData, required: true })
  @Get('/data/list')
  findAllData(@Query() query: ListDictData) {
    return this.dictService.findAllData(query);
  }

  @ApiOperation({ summary: '字典数据-详情' })
  @Get('/data/:id')
  @ApiParam({ name: 'id', required: true, description: '字典code' })
  findOneDictData(@Param('id') dictCode: string) {
    return this.dictService.findOneDictData(+dictCode);
  }

  @ApiOperation({ summary: '字典数据-类型-详情【走缓存】' })
  @Get('/data/type/:id')
  @ApiParam({ name: 'id', required: true, description: '字典类型ID' })
  findOneDataType(@Param('id') dictType: string) {
    return this.dictService.findOneDataType(dictType);
  }

  @ApiOperation({ summary: '导出字典组为xlsx文件' })
  @RequirePermission('system:dict:export')
  @Post('/type/export')
  @Operlog({ businessType: BusinessType.EXPORT })
  async export(@Res() res: Response, @Body() body: ListDictType): Promise<void> {
    return this.dictService.export(res, body);
  }

  @ApiOperation({ summary: '导出字典内容为xlsx文件' })
  @RequirePermission('system:dict:export')
  @Operlog({ businessType: BusinessType.EXPORT })
  @Post('/data/export')
  async exportData(@Res() res: Response, @Body() body: ListDictType): Promise<void> {
    return this.dictService.exportData(res, body);
  }
}
